<?php
/** All core functions listed here **/
/************************************/
/** DO NOT MODIFY THIS FILE **/

/* Getting the icons from the images folder */
function get_social_web_icons($size) {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    $icons = array();
    if($size == "size_16") {
        $currdir = $imgdir."/16x16";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/16x16";
        $userdir = $customdir."/social_web_links_icons/16x16";
        $user_icon_url_dir = $customurl."/social_web_links_icons/16x16";
    }
    else if($size == "size_24") {
        $currdir = $imgdir."/24x24";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/24x24";
        $userdir = $customdir."/social_web_links_icons/24x24";
        $user_icon_url_dir = $customurl."/social_web_links_icons/24x24";
    }
    else if($size == "size_32") {
        $currdir = $imgdir."/32x32";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/32x32";
        $userdir = $customdir."/social_web_links_icons/32x32";
        $user_icon_url_dir = $customurl."/social_web_links_icons/32x32";
    }
    else {
        $currdir = $imgdir."/48x48";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/48x48";
        $userdir = $customdir."/social_web_links_icons/48x48";
        $user_icon_url_dir = $customurl."/social_web_links_icons/48x48";
    }
    $dir = opendir($currdir);
    while(($file = readdir($dir)) !== false) {
        if(preg_match("/\.png/i",$file) > 0 or preg_match("/\.jpg/i",$file) > 0 or preg_match("/\.gif/i",$file) > 0 or preg_match("/\.jpeg/i",$file) > 0) {
            $name = strtolower(substr($file,0,strripos($file,".")));
            if(file_exists($userdir."/".$file)) {
                $icon_dir = $user_icon_url_dir;
            } else {
                $icon_dir = $icon_url_dir;
            }
            array_push($icons,array('name'=>$name,'icon'=>$file,'icon_url'=>$icon_dir.'/'.$file));
        }
    }
    closedir($dir);
    return $icons;
}

/* Create Horizontal display */
function get_horizontal_social_web_links() {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    $icon_array = get_social_web_icons($social_web_link_options['icon_size']);
    $output = '<div class="social_web_links">';
    foreach($icon_array as $social) {
        if(trim($social_web_link_options[$social['name']]) != "")
            $output .= '<a class="hsocial_icons" href="'.$social_web_link_options[$social['name']].'"><img src="'.$social['icon_url'].'" /></a>';
    }
    $output .= '</div>';
    return $output;
}

/* Create vertical display */
function get_vertical_social_web_links($num_of_col) {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    $icon_array = get_social_web_icons($social_web_link_options['icon_size']);    
    if($num_of_col == 1)
        $social_style_class = "social_one_col";
    else
        $social_style_class = "social_two_col";
    $output = '<div class="social_web_links_list">';
    foreach($icon_array as $social) {
        if(trim($social_web_link_options[$social['name']]) != "") {
            if($social_web_link_options['show_names'])
                $label = $social['name'];
            else $label = "";
            $output .= '<a class="'.$social_style_class.'" href="'.$social_web_link_options[$social['name']].'"><img style="vertical-align:middle; margin-right: 5px;" src="'.$social['icon_url'].'" />'.$label.'</a>'."\n";
        }
    }
    if($num_of_col > 1)
        $output .= '<div class="clear"></div>';
    $output .= '</div>';
    return $output;
}

/* Create bookmarks */
function get_social_web_links_bookmarks($size) {
    global $imgdir, $customdir, $customurl, $social_web_links_bookmarks, $social_web_link_options;
    global $post;
    $social_web_links_bookmarks = array(
            array('name' => 'facebook', 'bookmarkurl' => 'http://www.facebook.com/share.php?u='.get_permalink($post->ID).'&amp;t='.$post->post_title),
            array('name' => 'twitter', 'bookmarkurl' => 'http://twitter.com/home?status='.$post->post_title.'%20-%20'.get_permalink($post->ID)),
            array('name' => 'delicious', 'bookmarkurl' => 'http://delicious.com/post?url='.get_permalink($post->ID).'&amp;title='.$post->post_title.'&amp;notes='.$post->post_excerpt),
            array('name' => 'digg', 'bookmarkurl' => 'http://digg.com/submit?phase=2&amp;url='.get_permalink($post->ID).'&amp;title='.$post->post_title.'&amp;bodytext='.$post->post_excerpt),
            array('name' => 'google', 'bookmarkurl' => 'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk='.get_permalink($post->ID).'&amp;title='.$post->post_title.'&amp;annotation='.$post->post_excerpt),
            array('name' => 'reddit', 'bookmarkurl' => 'http://reddit.com/submit?url='.get_permalink($post->ID).'&amp;title='.$post->post_title),
            array('name' => 'stumbleupon', 'bookmarkurl' => 'http://www.stumbleupon.com/submit?url='.get_permalink($post->ID).'&amp;title='.$post->post_title),
            array('name' => 'email', 'bookmarkurl' => 'mailto:?subject='.$post->post_title.'&body='.get_permalink($post->ID))
    );
    if($size == "size_16") {
        $currdir = $imgdir."/16x16";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/16x16";
        $userdir = $customdir."/social_web_links_icons/16x16";
        $user_icon_url_dir = $customurl."/social_web_links_icons/16x16";
    }
    else if($size == "size_24") {
        $currdir = $imgdir."/24x24";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/24x24";
        $userdir = $customdir."/social_web_links_icons/24x24";
        $user_icon_url_dir = $customurl."/social_web_links_icons/24x24";
    }
    else if($size == "size_32") {
        $currdir = $imgdir."/32x32";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/32x32";
        $userdir = $customdir."/social_web_links_icons/32x32";
        $user_icon_url_dir = $customurl."/social_web_links_icons/32x32";
    }
    else {
        $currdir = $imgdir."/48x48";
        $icon_url_dir = get_bloginfo('wpurl')."/wp-content/plugins/social-web-links/images/48x48";
        $userdir = $customdir."/social_web_links_icons/48x48";
        $user_icon_url_dir = $customurl."/social_web_links_icons/48x48";
    }
    $output = '<div class="social_web_links_bookmarks"><span class="social_bookmark_label">'.__('Share this on').'</span>';
    foreach($social_web_links_bookmarks as $bookmark) {
        if(file_exists($userdir."/".$bookmark['name'].".png")) {
            $icon_dir = $user_icon_url_dir;
        } else {
            $icon_dir = $icon_url_dir;
        }
        $output .= '<a href="'.$bookmark['bookmarkurl'].'"><img src="'.$icon_dir.'/'.$bookmark['name'].'.png" /></a>';
    }
    $output .= '</div>';
    return $output;
}

function add_bookmarks_to_content($content) {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
        if(is_single())
            $content .= get_social_web_links_bookmarks($social_web_link_options['bookmark_icon_size']);
    return $content;
}

/* Creating function to replace tag in post or page */
function add_links_to_content($content) {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    if($social_web_link_options['display_style'] == "horizontal")
        $replace = '<div class="social_web_links">'.get_horizontal_social_web_links().'</div>';
    else if($social_web_link_options['display_style'] == "vertical_one")
        $replace = '<div class="social_web_links">'.get_vertical_social_web_links(1).'</div>';
    else
        $replace = '<div class="social_web_links">'.get_vertical_social_web_links(2).'</div>';
    $content = str_replace('[social-web-links-tag]',$replace,$content);
    return $content;
}

/* Get default options */
function get_social_web_links_settings() {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    $social_web_link_options = get_option('social_web_link_options');
    if(empty($social_web_link_options)) {
        $social_web_link_options['icon_size'] = "size_16";
        $social_web_link_options['display_style'] = "vertical_one";
        $social_web_link_options['widget_title'] = "Follow me";
        $social_web_link_options['show_names'] = true;
        $social_web_link_options['enable_bookmarks'] = true;
        $social_web_link_options['bookmark_icon_size'] = "size_16";
    }
}

/* Creating widgets and widget control */
function social_web_links_widget($args) {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    extract($args);
    echo $before_widget;
    echo $before_title;
    echo $social_web_link_options['widget_title'];
    echo $after_title;
    if($social_web_link_options['display_style'] == "horizontal") {
        echo get_horizontal_social_web_links();
    } else if($social_web_link_options['display_style'] == "vertical_one") {
        echo get_vertical_social_web_links(1);
    } else {
        echo get_vertical_social_web_links(2);
    }    
    echo $after_widget;
}

function social_web_links_widget_control() {
    global $imgdir, $customdir, $customurl, $social_web_link_options;
    if($_POST['social_web_links_widget_submit']) {
        $social_web_link_options['widget_title'] = $_POST['social_web_links_widget_title'];
        update_option('social_web_link_options',$social_web_link_options);
    }
    echo '<p><label>'.__('Title').'</label></p>';
    echo '<p><input style="width: 200px;" id="social_web_links_widget_title" name="social_web_links_widget_title" type="text" value="' . $social_web_link_options['widget_title'] . '" /></p>';
    echo '<input type="hidden" id="social_web_links_widget_submit" name="social_web_links_widget_submit" value="1" />';
}
?>